<template>
  <div class="wiper">
      <Filter title="配送单" :items="filterItems" :model="filterModel" />
      <Table :buttons="buttons" :columns="columns" :data="data" :pagination="filterModel" row-key="code"/>
  </div>
</template>

<script>
import { message, Modal } from "ant-design-vue";
import Filter from "@/components/Filter";
import Table from "@/components/Table";
import { pager } from "@/api/order/delivery";
import { ColumnProps, FilterItems, FilterCondition } from "./config";
import { Dictionary } from "@/utils/Dictionary";

export default {
  name: 'delivery',
  data() {
      return { 
          filterItems: FilterItems,
          data: [],
          columns: ColumnProps,
          filterModel: {
              ...FilterCondition,
              onChange: (page, pageSize) => {
                  this.filterModel.current = page;
                  this.filterModel.pageSize = pageSize;
                  this.load();
              }
          },
      };
  },
  components: {
      Filter,
      Table
  },
  computed: {
      isDictionary() {
          return this.$store.state.dictionary;
      },
  },
  watch: {
      isDictionary(data) {
          this._deliveryType(data);
      },
  },
  mounted() {
      this.load();
      this._deliveryType(this.$store.state.dictionary);
  },
  methods: {
      /**
       * 加载数据
       */
      load() {
        console.log(this.filterModel.current)
        pager(this.filterModel).then(res => {
            this.data = res.data.rows;
            this.filterModel.total = res.data.total;
          console.log(this.filterModel.current);
            console.log('data',this.data);

        }).catch(err => {
            message.error('加载数据失败');
            console.error(err);
        });

},

      /**
       * 配送类型和状态
       * @param data
       * @private
       */
      _deliveryType(data) {
          const deliveryTypes = this._rename(data.filter(item => item.code === Dictionary.DELIVERY_TYPE)[0]?.children || [], "code");
          const deliveryStatus = this._rename(data.filter(item => item.code === Dictionary.DELIVERY_STATUS)[0]?.children || [], "code");
          this.filterItems.find(item => item.key === 'type').data = deliveryTypes;
          this.filterItems.find(item => item.key === 'status').data = deliveryStatus;
      },
      /**
       * 跳转路由
       */
      goRoute(ids) {
          return new Promise((resolve, reject) => {
              if (ids.length === 0) {
                  return reject("至少要选择一条数据");
              }
              if (ids.length > 1) {
                  return reject("只能选择一条数据");
              }
              return resolve();
          });
      },
      /**
       * 刷新
       */
      refresh() {
          this.load();
      },
    /**
             * 重命名
             * @private
             */
             _rename(list, key = "id"){
                return list?.map(item => {
                    return {label: item.name, value: item[key],children: this._rename(item.children)}
                });
            },
  }
};
</script>

<style scoped lang="scss">
/* 添加样式 */
</style>
